<?php
namespace Portal\Controller;
use Think\Controller\RestController;

class GuideController extends RestController {
  function _initialize() {
      $api_m=M('api');
      $uri = substr($_SERVER['SCRIPT_URI'],48);
      $ret = $api_m->where("uri = '%s'",$uri)->select();
      
      if ($ret) {
        $data['uri'] = $uri;
        $data['freq'] = (int)$ret[0]['freq']+1;
        $api_m->where("uri = '%s'",$uri)->save($data);
      } else {
        $data['uri'] = $uri;
        $data['freq'] = 1;
        $api_m->where("uri = '%s'",$uri)->add($data);
      }
        //print_r($_SERVER['SCRIPT_URI']);
  }
  public function getnearsite() {
    $post_obj=M("Posts");
    $result=$post_obj->order('post_hits DESC')->select();
    $resp=array();

    foreach ($result as $r) {
      if ($this->isnear($_GET['lat'],$_GET['lng'],$r['lat'],$r['lng'],isset($_GET['t'])?$_GET['t']:1)) {
        $smeta = json_decode($r["smeta"],true);
        $r["audio"] = $smeta["audio"][0]["url"]?"http://hellomythinkphp-data.stor.sinaapp.com/upload/".$smeta["audio"][0]["url"]:"";
        $r["thumb"] = $smeta["thumb"]?"http://hellomythinkphp-data.stor.sinaapp.com/upload/".$smeta["thumb"]:"";
        unset($r['smeta']);
        array_push($resp, $r);
      }
    }
    $this->response($resp,'json');
  }
  private function isnear($lat1,$lng1,$lat2,$lng2,$thre=1) {
    if (!$lat2||!$lng2) return false;
    if (abs($lat1-$lat2)<$thre && abs($lng1-$lng2)<$thre){
      return true;
    }
    else
      return false;
  }
  public function test() {
    $info = array(
        array("title" => "G1","info" => "google 1","id" => "3"
          ),
        array("title" => "G2","info" => "google 2","id" => "3"
          ),
        array("title" => "G4","info" => "google 3","id" => "3"
          )
        );
    $this->response($info,'json');
  }

  public function getallsite() {
    $post_obj=M('Posts');
    $result=$post_obj->where('post_parent = 0')->order('post_hits DESC')->select();
    $resp=array();
    
    foreach ($result as $r) {
        //var_dump($r);
      $smeta = json_decode($r["smeta"],true);
      $r["audio"] = $smeta["audio"][0]["url"]?"http://hellomythinkphp-data.stor.sinaapp.com/upload/".$smeta["audio"][0]["url"]:"";
      $r["thumb"] = $smeta["thumb"]?"http://hellomythinkphp-data.stor.sinaapp.com/upload/".$smeta["thumb"]:"";
      unset($r['smeta']);
      array_push($resp, $r);
    }
    $this->response($resp,'json');
  }

  public function getallchild() {
    $post_obj=M('Posts');
    $result=$post_obj->where('post_parent = '.$_GET['parentid'])->order('post_hits DESC')->select();
    $resp=array();

    foreach ($result as $r) {
        //var_dump($r);
      $smeta = json_decode($r["smeta"],true);
      $r["audio"] = $smeta["audio"][0]["url"]?"http://hellomythinkphp-data.stor.sinaapp.com/upload/".$smeta["audio"][0]["url"]:"";
      $r["thumb"] = $smeta["thumb"]?"http://hellomythinkphp-data.stor.sinaapp.com/upload/".$smeta["thumb"]:"";
      unset($r['smeta']);
      array_push($resp, $r);
    }
    $this->response($resp,'json');
  }
    
  public function statistic() {
    $stat_model=M('Statistic');
    $post_model=M('Posts');

    $post_model->where('id = '.$_GET['postid'])->setInc('post_hits');
    //$post_model=M('Posts');
    $where['post_id']=$_GET['postid'];
    $where['date']=date('Y-m-d');
    $where['_logic']='AND';
    //print_r($where);
    $result = $stat_model->where($where)->select();
    //print_r($result);

    if ($result) {
      $result = $result[0];
      $result['hit'] += 1;
      $result['timelen'] += $_GET['timelen'];
    }
    else {
      $result['hit'] = 1;
      $result['timelen'] += $_GET['timelen'];
      $result['post_id'] = $_GET['postid'];
      $result['date'] = date('Y-m-d');
    }
    $stat_model->add($result,NULL,true);
    $this->response($result,'json');
  }

  public function testdata() {
    $page=sp_sql_page($_GET['id']);
    $smeta = json_decode($page["smeta"],true);
    //var_dump($smeta["photo"]);
      
    $resp["audio"] = "http://hellomythinkphp-data.stor.sinaapp.com/upload/".$smeta["audio"][0]["url"];
    $resp["thumb"] = "http://hellomythinkphp-data.stor.sinaapp.com/upload/".$smeta["thumb"];
    $resp["title"] = $page["post_title"];
    $resp["content"] = $page["post_content"];
    $this->response($resp,'json');
  }

  public function register() {
    $kv = new \SaeKV();
    $ret = $kv->init();

    $users_model=M("Users");
    extract($_GET);
    $where['user_login']=$username;
    $where['user_email']=$email;
    $where['_logic'] = 'OR';

    $result = $users_model->where($where)->count();
    if ($result) {
      $resp['state'] = 0;
    } else {
      $need_email_active=C("SP_MEMBER_EMAIL_ACTIVE");
      $data=array(
          'user_login' => $username,
          'user_email' => $email,
          'user_nicename' =>$username,
          'user_pass' => sp_password($password),
          'last_login_ip' => get_client_ip(),
          'create_time' => date("Y-m-d H:i:s"),
          'last_login_time' => date("Y-m-d H:i:s"),
          'user_status' => $need_email_active?2:1,
          "user_type"=>2,
          );
      $rst = $users_model->add($data);
      if($rst){
        $resp['state'] = 1;
        $resp['id'] = $rst;
        $token = md5($username.time().'smartguide');
        $ret = $kv->add($result['id'],$token);
        if ($ret == false) $ret = $kv->set($result['id'],$token);
        $resp['token'] = $token;
      } else {
        $resp['state'] = 0;
      }
    }
    $this->response($resp,'json');
  }

  public function login() {
    $kv = new \SaeKV();
    $ret = $kv->init();

    $users_model=M("Users");
    $where['user_login']=$_GET['username'];
    $where['user_pass']=sp_password($_GET['password']);
    $where['_logic'] = 'AND';
    $result = $users_model->where($where)->count();

    if ($result) {
      $result = $users_model->where($where)->find();
      $data = array(
          'last_login_time' => date("Y-m-d H:i:s"),
          'last_login_ip' => get_client_ip(),
          );
      $users_model->where("id=".$result['id'])->save($data);

      $token = md5($_GET['username'].time().'smartguide');
      $ret = $kv->add($result['id'],$token);
      if ($ret == false) $ret = $kv->set($result['id'],$token);

      $resp['state'] = 1;
      $resp['userid'] = $result['id'];
      $resp['token'] = $token;

      //$this->response($resp,'json');
    } else {
      $resp['state'] = 0;
    }
    $this->response($resp,'json');
  }

  public function getdata() {
    $kv = new \SaeKV();
    $ret = $kv->init();

    $token = $kv->get($_GET['userid']);
    if ($token == $_GET['token']) {
      $page=sp_sql_page($_GET['postid']);
      $this->response($page,'json');
    } else {
      $this->response("error",'json');
    }// echo sp_is_user_login();
    //$j=json_encode($page);
    //print_r($page);
    //print_r($j);
    //print_r(json_decode($j));
  }

}

?>
